﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Orange.Mvc4App.Infrastructure;
using System.Diagnostics;

namespace Orange.Mvc4App {
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            //ViewEngines.Engines.Add(new OrangeViewEngine());
            //ViewEngines.Engines.Insert(0, new OrangeViewEngine());

            filters.Add(new HandleErrorAttribute());
            filters.Add(new OrangeActionFilter());
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Customer"
                , "Customer/{pageIndex}"
                , new { controller = "Customer", action = "Index" }
                , new { pageIndex = @"\d+" }
            );

            routes.MapRoute(
                "Charge"
                , "Charge/Part{pageIndex}"
                , new { controller = "Charge", action = "Index" }
                , new { pageIndex = @"\d+" }
            );

            routes.MapRoute(
                "WaterOfCharge"
                , "Charge/Water{pageIndex}"
                , new { controller = "Charge", action = "Water" }
                , new { pageIndex = @"\d+" }
            );

            routes.MapRoute(
               "ElecOfCharge"
               , "Charge/Elec{pageIndex}"
               , new { controller = "Charge", action = "Elec" }
               , new { pageIndex = @"\d+" }
           );

            routes.MapRoute(
                 "Default"
                 , "{controller}/{action}/{id}"
                 , new { controller = "Customer", action = "Index", id = UrlParameter.Optional }
                //, new { id = @"\d+" }
             );
        }

        protected void Application_Start() {
            // 初始化策略 (when model changes)
            //Database.SetInitializer<OrangeContext>(
            //    new DropCreateDatabaseIfModelChanges<OrangeContext>());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //DI
            //ControllerBuilder.Current.SetControllerFactory(new NinjectContollerFactory());
            DependencyResolver.SetResolver(new NinjectDependencyResolver());
        }

        protected void Session_Start(object sender, EventArgs e) {
            /*Two Enginner: one is webform view engine, the other is razer view engine*/
            //Debug.WriteLine(ViewEngines.Engines.Count);

        }

        /// <summary>
        /// 缓存定制
        /// </summary>
        /// <param name="context"></param>
        /// <param name="custom"></param>
        /// <returns></returns>
        //public override string GetVaryByCustomString(System.Web.HttpContext context, string custom) { }
    }
}